<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Modules {
    
    function Modules() {
        log_message('debug', "Modules Class Initialized");
    }
    
    function run($module_path = '', $method_init = NULL, $return = TRUE, $constructor_init = NULL) {
        $CI = & get_instance();
        
        if (($pos = strrpos($module_path, '/')) != FALSE) {
            $module_method = substr($module_path, $pos + 1);
            $module_controller = substr($module_path, 0, $pos);
        }

        $path = APPPATH . 'controllers/f/' . $module_controller . EXT;

        if (!file_exists($path)) {
            show_error('Unable to locate the module you have specified: ' . $module_path . EXT);
        }

        require_once($path);
        $module_controller = ucfirst($module_controller);
        if (!is_null($constructor_init))
            $obj = new $module_controller($constructor_init);
        else
            $obj = new $module_controller;
        if (method_exists($obj, $module_method)) {
            if (!is_null($method_init))
                $obj->$module_method($method_init);
            else
                $obj->$module_method();
        } else
            show_error('Unable to locate the module method you have specified: ' . $module_method . EXT);
    }
    
    function search_path($module_path = '') {
        $CI = & get_instance();

        $site = FRONT_SITE;
        if (stripos(strtolower($module_path), strtolower(ADMIN_SITE)) === 0) {
            $site = ADMIN_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(ADMIN_SITE) . '.', '', strtolower($module_path)));
        } elseif(stripos(strtolower($module_path), strtolower(CLIENT_SITE)) === 0) {
            $site = CLIENT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(CLIENT_SITE) . '.', '', strtolower($module_path)));
        } elseif(stripos(strtolower($module_path), strtolower(FRONT_SITE)) === 0) {
            $site = FRONT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(1):(str_replace(strtolower(FRONT_SITE) . '.', '', strtolower($module_path)));
        } elseif(stripos(strtolower($CI->uri->segment(1)), 'admin') === 0 || @strtolower(trim($_GET[$CI->config->item('function_trigger')])) == 'admin') {
            $site = ADMIN_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(ADMIN_SITE) . '.', '', strtolower($module_path)));
        } elseif(stripos(strtolower($CI->uri->segment(1)), 'client') === 0 || @strtolower(trim($_GET[$CI->config->item('function_trigger')])) == 'client') {
            $site = CLIENT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(2):(str_replace(strtolower(CLIENT_SITE) . '.', '', strtolower($module_path)));
        } else {
            $site = FRONT_SITE;
            $module_path = empty($module_path)?$CI->uri->segment(1):(str_replace(strtolower(FRONT_SITE) . '.', '', strtolower($module_path)));
        }

        $arr = explode('.', $module_path);
        if (count($arr) == 1) {
            $arr[1] = $arr[0];
            $arr[2] = 'index';
        }

        if (count($arr) == 2) {
            $arr[2] = 'index';
        }

        $arr[3] = $site;
        return $arr;
    }
}
